package com.dfrz.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfrz.bean.User;
import com.dfrz.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Create by keyan on 2021/7/29 11:04
 */

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    UserMapper userMapper;

    @Override
    public int addUser(User user) {
        user.setFlag(1);
        int i = userMapper.insert(user);
        return i;
    }

    @Override
    public List<User> getUsers() {
        QueryWrapper qw  = new QueryWrapper();
        qw.eq("flag",1);
        List<User> userList = userMapper.selectList(qw);
        return userList;
    }

    @Override
    public int delUserById(Integer id) {
        UpdateWrapper<User> qw = new UpdateWrapper<>();
        qw.eq("id",id);
        qw.set("flag",-1);
        int i = userMapper.update(new User(),qw);
        return i;
    }

    @Override
    public List<User> getUsersParm(Map<String, String> parm) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("flag",1);
        if(parm.get("uname")!=null && !parm.get("uname").trim().equals("")){
            qw.like("uname",parm.get("uname"));
            return userMapper.selectList(qw);
        }
        return userMapper.selectList(qw);
    }

    @Override
    public IPage<User> getUserByPage(Map<String, String> parm, Integer pageNo, Integer pageSize) {
        IPage<User> pages = new Page<>();
        pages.setCurrent(pageNo);
        pages.setSize(pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("flag",1);
        if(parm.get("uname")!=null && !parm.get("uname").trim().equals("")){
            qw.like("uname",parm.get("uname"));
        }
        if(parm.get("gender")!=null && !parm.get("gender").trim().equals("")){
            qw.eq("gender",parm.get("gender"));
        }
        return  userMapper.selectPage(pages,qw);
    }

    @Override
    public int updateUser(User user) {
        if (user.getId()==null){
            return 0;
        }
        int i = userMapper.updateByPrimaryKeySelective(user);

        return i;
    }

    @Override
    public User getUserById(Integer id) {
        return  userMapper.selectById(id);
    }

    @Override
    public List<Map<String, String>> statisticalRole() {
        List<Map<String, String>> maps = userMapper.statisticalRole();

        return maps;
    }


}
